package com.yunkaweilai.android.activity.operation.consumption.guadan;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.operation.consumption.shops.ShopOrTimeQrConsumptionActivity;
import com.yunkaweilai.android.activity.operation.consumption.shops.ShopOrTimeSearchActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.fragment.guadan.GuadanPtShopTypeFragment;
import com.yunkaweilai.android.fragment.guadan.GuadanTimeShopTypeFragment;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.zhy.m.permission.MPermissions;
import com.zhy.m.permission.PermissionDenied;
import com.zhy.m.permission.PermissionGrant;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 挂单商品消费
 * Created by Administrator on 2017/7/18.
 */

public class GuadanShopConsumptionActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.id_edt_search)
    TextView idEdtSearch;
    @BindView(R.id.id_llayout_qr)
    LinearLayout idLlayoutQr;
    @BindView(R.id.id_rbtn_1)
    RadioButton idRbtn1;
    @BindView(R.id.id_rbtn_2)
    RadioButton idRbtn2;
    @BindView(R.id.id_group)
    RadioGroup idGroup;
    @BindView(R.id.id_shop_container)
    FrameLayout idShopContainer;
    @BindView(R.id.id_shop_llayout)
    LinearLayout idShopLlayout;
    public static final String GUADAN_GID = "GUADAN_GID";
    @BindView(R.id.id_view1)
    View idView1;
    @BindView(R.id.id_view2)
    View idView2;
    private String gid;
    private GuadanPtShopTypeFragment ptShopTypeFragment;
    private GuadanTimeShopTypeFragment timeShopTypeFragment;

    public static void actionStart(Context context, String gid) {
        Intent intent = new Intent(context, GuadanShopConsumptionActivity.class);
        intent.putExtra(GUADAN_GID, gid);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_consumption);
        ButterKnife.bind(this);
        gid = getIntent().getStringExtra(GUADAN_GID);
        new TitleBuilder(mAc).setTitleText("选择商品")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        idGroup.setOnCheckedChangeListener(this);
        idRbtn1.setChecked(true);
        rbCheck(true);

    }

    private void rbCheck(boolean flag) {
        if (flag) {
            idEdtSearch.setHint("请输入实物商品/条码");
        } else {
            idEdtSearch.setHint("请输入计次名称/条码");
        }
    }

    public void hideAllFragment(FragmentTransaction transaction) {
        if (ptShopTypeFragment != null) {
            transaction.hide(ptShopTypeFragment);
        }
        if (timeShopTypeFragment != null) {
            transaction.hide(timeShopTypeFragment);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch (checkedId) {
            case R.id.id_rbtn_1:
                rbCheck(true);
                if (ptShopTypeFragment == null) {
                    ptShopTypeFragment = GuadanPtShopTypeFragment.newInstance(gid);
                    transaction.add(R.id.id_shop_container, ptShopTypeFragment);
                } else {
                    transaction.show(ptShopTypeFragment);
                }
                idView1.setBackgroundColor(getResources().getColor(R.color.wjx_title_bg_next));
                idView2.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                break;
            case R.id.id_rbtn_2:
                rbCheck(false);
                if (timeShopTypeFragment == null) {
                    timeShopTypeFragment = GuadanTimeShopTypeFragment.newInstance(gid);
                    transaction.add(R.id.id_shop_container, timeShopTypeFragment);
                } else {
                    transaction.show(timeShopTypeFragment);
                }
                idView2.setBackgroundColor(getResources().getColor(R.color.wjx_title_bg_next));
                idView1.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                break;
        }
        transaction.commit();
    }

    @OnClick({R.id.id_edt_search, R.id.id_llayout_qr})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_edt_search://商品搜索
                ShopOrTimeSearchActivity.actionStart(mAc,"");
                break;
            case R.id.id_llayout_qr://扫码界面
                getCameraManifest();
                break;
        }
    }

    //获取照相机权限
    private static final int MY_PERMISSIONS_REQUEST_CAMERA = 1;

    private void getCameraManifest() {
        MPermissions.requestPermissions(this,MY_PERMISSIONS_REQUEST_CAMERA, Manifest.permission.CAMERA);
    }

    private void getCamera() {
        ShopOrTimeQrConsumptionActivity.actionStart(this,"");
    }

    @PermissionGrant(MY_PERMISSIONS_REQUEST_CAMERA)
    public void requestSuccess(){
        getCamera();
    }
    @PermissionDenied(MY_PERMISSIONS_REQUEST_CAMERA)
    public void requestError(){
        showToast("授权失败！");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        MPermissions.onRequestPermissionsResult(this,requestCode,permissions,grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }
}
